package org.balthie.demo.opensource.quartz.dealexception;

import java.util.Date;
import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.SimpleTrigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JobExceptionExample
{
    public void run() throws Exception
    {
        Logger log = LoggerFactory.getLogger(JobExceptionExample.class);
        
        log.info("------- Initializing ----------------------");
        
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();
        
        log.info("------- Initialization Complete ------------");
        
        log.info("------- Scheduling Jobs -------------------");
        
        Date startTime = DateBuilder.nextGivenSecondDate(null, 15);
        
        JobDetail job = JobBuilder.newJob(BadJob1.class).withIdentity("badJob1", "group1").usingJobData("denominator", "0")
                .build();
        
        SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startAt(startTime)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10).repeatForever()).build();
        
        Date ft = sched.scheduleJob(job, trigger);
        log.info(job.getKey()
                + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
                + (trigger.getRepeatInterval() / 1000L) + " seconds");
        
        job = JobBuilder.newJob(BadJob2.class).withIdentity("badJob2", "group1").build();
        
        trigger = (SimpleTrigger) TriggerBuilder.newTrigger().withIdentity("trigger2", "group1").startAt(startTime)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever()).build();
        
        ft = sched.scheduleJob(job, trigger);
        log.info(job.getKey()
                + " will run at: " + ft + " and repeat: " + trigger.getRepeatCount() + " times, every "
                + (trigger.getRepeatInterval() / 1000L) + " seconds");
        
        log.info("------- Starting Scheduler ----------------");
        
        sched.start();
        
        log.info("------- Started Scheduler -----------------");
        try
        {
            Thread.sleep(30000L);
        }
        catch (Exception e)
        {
        }
        log.info("------- Shutting Down ---------------------");
        
        sched.shutdown(false);
        
        log.info("------- Shutdown Complete -----------------");
        
        SchedulerMetaData metaData = sched.getMetaData();
        log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
    }
    
    public static void main(String[] args) throws Exception
    {
        JobExceptionExample example = new JobExceptionExample();
        example.run();
    }
}